package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CutPageBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IFacilitiesMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private IRoomMapper mapper;

    @Autowired
    private IStudentMapper stuMapper;

    @Autowired
    private IFacilitiesMapper facMapper;

    @Override
    public IPage<RoomBean> findByItem(int pageNO, String address, String state, String info, String type) {
        return mapper.findByItem(new Page(pageNO,3),address,state,info,type);
    }

    @Override
    public void add(RoomBean room) {
        mapper.insert(room);
    }

    @Override
    public boolean del(Integer roomId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_roomId",roomId);

        Integer num=stuMapper.selectCount(qw);
        if (num ==0){
            facMapper.delete(qw);
            mapper.deleteById(roomId);
            return true;
        }
        else {
            return false;
        }

    }

    @Override
    public RoomBean findByRoomId(Integer id) {
       RoomBean room = mapper.findByRoomId(id);
       room.setStuList(stuMapper.findStudentByRoomId(id));
       return room;

    }

    @Override
    public List<RoomBean> findByGender(String gender) {
        return mapper.findByGender(gender);
    }
}
